﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using DateBaseConnectionPool;

namespace TestMySqlConnetPoll
{
    public struct objRunStruct
    {
        public int startTime;   //程序启动时间
        public int overTime;    //程序结束时间
    }
    public class ProgarmRunTimeTable
    {
        public Dictionary<objDate, List<objRunStruct>> RunTimeList = new Dictionary<objDate, List<objRunStruct>>();
        public bool ReadyRead = false;
        public int ErrorFlag = 0;
        private Mutex keyMutex = new Mutex();
        static private int keykey = 0;
        public void Read_TimeList()
        {
            ErrorFlag = 0;
            ReadyRead = false;
            RunTimeList.Clear();
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadTimeList), null);
        }

        public void ThreadReadTimeList(object o)
        {
            string TableName = "ProgramRunTime";
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder SelectStatement = new StringBuilder();

                    SelectStatement.Append(string.Format("select start_time,end_time from {0}",TableName));                                                       
                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时

                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            objRunStruct obj = new objRunStruct();
                            obj.startTime = read.GetInt32(read.GetOrdinal("start_time"));
                            obj.overTime = read.GetInt32(read.GetOrdinal("end_time"));
                            DateTime t = MySql.Time_tToDateTime(obj.startTime);
                            objDate okey = new objDate();
                            okey.year = t.Year;
                            okey.month = (byte)t.Month;
                            okey.day = (byte)t.Day;
                            if(RunTimeList.Keys.Contains(okey))
                            {
                                RunTimeList[okey].Add(obj);
                            }
                            else
                            {
                                List<objRunStruct>li = new List<objRunStruct>();
                                li.Add(obj);
                                RunTimeList.Add(okey,li);
                            }                            
                        }
                        read.Close();
                        ReadyRead = true;
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 3;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }
    }
}
